﻿using System;
using System.Web.Security;
using Data;


namespace MySimpleDiary.Models
{
   

    public interface IMembershipService
    {
        bool ValidateUser(string userName, string password);
        MembershipCreateStatus CreateUser(string userName, string password, string email);
    }

    public class AccountMembershipService : IMembershipService
    {
        private readonly MembershipProvider Provider;

        public AccountMembershipService()
            : this(null)
        {
        }

        public AccountMembershipService(MembershipProvider provider)
        {
            Provider = provider ?? Membership.Provider;
        }

        public bool ValidateUser(string userName, string password)
        {
            // Check that we're getting valid data from the Login screen.
            if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName");
            if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", "password");

            //Call the custom user validator, which I created and placed in a shared User Repository.
            return SiteUserRepository.ValidateUser(userName, password);
        }

        public MembershipCreateStatus CreateUser(string userName, string password, string email)
        {
            MembershipCreateStatus status = MembershipCreateStatus.ProviderError;

            SiteUserRepository.CreateUser(userName, password, null, null, null, 1);

            return status;
        }

    }

    public interface IFormsAuthenticationService
    {
        void SignIn(string userName, bool createPersistentCookie);
        void SignOut();
    }


    public class FormsAuthenticationService : IFormsAuthenticationService
    {
        /// <summary>
        /// This seems to handle the Forms Authentication. If the user is verified, this seems to handle the Forms.Authenticated bit.
        /// </summary>
        /// <param name="userName">The users Username</param>
        /// <param name="createPersistentCookie">Was Remember Me selected? If so, store the login in a cookie.</param>
        public void SignIn(string userName, bool createPersistentCookie)
        {
            if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName");

            FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
        }

        public void SignOut()
        {
            FormsAuthentication.SignOut();
        }
    }

}